// SKNotes - a note taking and gathering tool
// Copyright (C) 2009  Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.Runtime.InteropServices;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;

namespace SKNotes.AsyncProtocolHandler
{
    [Guid("E0769D68-E903-4f76-AAC4-9632971547E6")]
	public class NotesProtocol : PluggableProtocolHandler
	{
        public static readonly string Schema="sknotes";

        public NotesProtocol()
		{
		}

        protected override void OnProtocolStart(EventArgs e)
        {
            string zipFilePath = NoteList.Instance.CurrentNotebook.Path + "\\" + Request.Url.Host;

            if (File.Exists(zipFilePath))
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        if ((theEntry.IsFile) && (theEntry.Name == "inlines/"+Request.Url.Segments[1]))
                        {
                            // Try to detect ContentType
                            try
                            {
                                string ext = Path.GetExtension(theEntry.Name);
                                if (ext != "")
                                {
                                    RegistryKey extKey = Registry.ClassesRoot.OpenSubKey(ext);
                                    if (extKey != null)
                                    {
                                        string contentType = extKey.GetValue("Content Type") as string;
                                        if ((contentType != null) && (contentType.Length > 0))
                                            contentType = extKey.GetValue("PerceivedType") as string;

                                        if (contentType != null)
                                            Response.ContentType = contentType;
                                    }
                                }
                            }
                            catch { }

                            // send the data
                            byte[] data = new byte[4096];
                            int size = s.Read(data, 0, data.Length);
                            while (size > 0)
                            {
                                Response.OutputStream.Write(data, 0, size);
                                size = s.Read(data, 0, data.Length);
                            }
                            break;
                        }
                    }

                    // Close can be omitted as the using statement will do it automatically
                    // but leaving it here reminds you that is should be done.
                    s.Close();
                }

                Response.EndResponse();
            }
        }
    }
}
